<?php

namespace App\Model\Order;

use App\Core\BaseModel;
use App\Core\Exception\DefaultException;
use App\Model\Member\MemberModel;

/**
 * CREATE TABLE `ss_order_task` (
 * `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id',
 * `user_id` int(11) NOT NULL COMMENT '用户id',
 * `quantity` smallint(3) NOT NULL DEFAULT '0' COMMENT '下单数量',
 * `order_count` smallint(3) NOT NULL DEFAULT '0' COMMENT '已下单数量-废弃字段,用上一个字段',
 * `order_amount` decimal(12,2) NOT NULL DEFAULT '0.00' COMMENT '下单总金额',
 * `user_profit` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '用户单笔提成',
 * `finished_order_count` smallint(3) NOT NULL DEFAULT '0' COMMENT '已退款订单数量',
 * `total_user_profit` decimal(10,2) NOT NULL COMMENT '已获得提成总额',
 * `history` text COMMENT '下单记录',
 * `created_at` datetime NOT NULL DEFAULT '1970-01-01 00:00:00' COMMENT '创建时间',
 * `created_by` varchar(32) NOT NULL DEFAULT '' COMMENT '创建人员账号',
 * `updated_at` datetime NOT NULL DEFAULT '1970-01-01 00:00:00' ON UPDATE CURRENT_TIMESTAMP COMMENT '最后更新时间',
 * `updated_by` varchar(32) NOT NULL DEFAULT '' COMMENT '最后更新人员账号',
 * `is_delete` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否删除[0:未删除，1：已删除]',
 * `deleted_at` datetime NOT NULL DEFAULT '1970-01-01 00:00:00' COMMENT '删除时间',
 * PRIMARY KEY (`id`) USING BTREE
 * ) ENGINE=InnoDB AUTO_INCREMENT=19363 DEFAULT CHARSET=utf8mb4 COMMENT='下单任务';
*/
/**
 * Class OrderTaskModel
 * @property int id
 * @property int user_id
 * @property int quantity
 * @property int order_count
 * @property float order_amount
 * @property float user_profit
 * @property int finished_order_count
 * @property float total_user_profit
 * @property string history
 * @property string created_at
 */

class OrderTaskModel extends BaseModel
{
    protected $tableName = 'ss_order_task';

    protected $casts = [
        'id'          => 'integer',
        'user_id'     => 'integer',
        'quantity'    => 'integer',
        'order_count' => 'integer',
        'order_amount'=> 'float',
        'user_profit' => 'float',
        'finished_order_count' => 'integer',
        'total_user_profit' => 'float',
        'history'     => 'string',
        'created_at'  => 'string',
    ];

    const COUNTLIMIT = 100;

    public function member()
    {
        return $this->hasOne(MemberModel::class, function ($query) {
            $query->where('is_delete', 0);
            return $query;
        }, 'user_id', 'id');
    }
    protected static function onAfterUpdate($model, $res)
    {
        self::delCache('orderTaskCache:');
    }

    protected static function onAfterInsert($model, $res)
    {
        self::delCache('orderTaskCache:');
    }
}
